package com.yeban.algorithm;

/**
 * @Desc TODO
 * @Author yeban
 * @Date 2025/10/29
 *
 * 36. 有效的数独
 */
public class test36 {
    public boolean isValidSudoku(char[][] board) {
        // 初始化记录器
        int[][] rows = new int[9][9];
        int[][] cols = new int[9][9];
        int[][][] subbox = new int[3][3][9];

        // 遍历棋盘
        for(int i = 0; i < 9; i ++) {
            for(int j = 0; j < 9; j ++) {
                char c = board[i][j];
                if(c != '.') {
                    int index = c - '0' - 1;
                    rows[i][index]++;
                    cols[j][index]++;
                    subbox[i/3][j/3][index]++;
                    // 去重
                    if(rows[i][index] > 1 || cols[j][index] > 1 || subbox[i/3][j/3][index] > 1) {
                        return false;
                    }
                }
            }
        }
        return true;
    }
}
